package com.zp.qualitypractice.rx;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.elvishew.xlog.XLog;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zp.qualitypractice.ALog;
import com.zp.qualitypractice.App;
import com.zp.qualitypractice.Bean.User;
import com.zp.qualitypractice.R;
import com.zp.qualitypractice.fragment.LazyFragment2;
import com.zp.qualitypractice.rx.event.EventA;
import com.zp.qualitypractice.rx.event.EventBus;
import com.zp.qualitypractice.rx.event.EventMain;
import com.zp.qualitypractice.rx.event.Subscribe;
import com.zp.qualitypractice.rx.event.ThreadMode;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by izp on 2017/8/14.
 */

public class RxDemoActivity extends RxAppCompatActivity {
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.button5)
    Button button5;
    @BindView(R.id.button6)
    Button button6;
    @BindView(R.id.button7)
    Button button7;
    @BindView(R.id.rootView)
    ConstraintLayout rootView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        ButterKnife.bind(this);

        Bus.getBusOrdinary().toObservable().subscribe(new Consumer<Object>() {
            @Override
            public void accept(@NonNull Object o) throws Exception {
                editText.setText(o.toString());
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bus.getBusOrdinary().post("测试文字");
                startActivity(new Intent(RxDemoActivity.this,RxBeginActivity.class));
            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ALog.d("发送！！");

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ALog.d("发送！！");
                        EventMain eventMain = new EventMain(RxDemoActivity.this);
                        Bundle bundle = new Bundle();
                        bundle.putString("bundle","测试bundle");
                        bundle.putParcelable("user",new User().setNickname("vicky").setCpname("张鹏"));
                        EventBus.getDefault().post(eventMain.setBundle(bundle));
                    }
                }).start();

            }
        });
        EventBus.getDefault().register(this);

    }
    @Override
    protected void onStart() {
        super.onStart();
        ALog.d("后台监听"+ App.getApp().getAppCount());
    }

    @Override
    protected void onStop() {
        super.onStop();
        ALog.d("后台监听"+App.getApp().getAppCount());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe()
    public void receiveEventMain(EventMain event) {
        XLog.d(event.getMsg());
        editText.setText(((User)event.getBundle().getParcelable("user")).getCpname());
    }
}
